<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;

class Course extends Backend{
    protected $model = null;
    protected $searchFields = 'id';
    protected $bankList = [];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Course');
        
        $bankList = \think\Db::name("course")->order('id ASC')->select();
        
        Tree::instance()->init($bankList)->icon = ['    ', '    ', '    '];
        $this->banklist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');

        $bankdata = [0 => __('None')];
        $parentdata = [0 => __('None')];
        foreach ($this->banklist as $k => &$v) {
            if (!$v['pid']) {
                $parentdata[$v['id']] = $v['name'];
            }
            $bankdata[$v['id']] = $v['name'];
        }
        unset($v);
        $this->view->assign('bankdata', $bankdata);
        $this->view->assign('parentdata', $parentdata);
    }

    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            Tree::instance()->init($list)->icon = ['    ', '    ', '    '];
            $this->list = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
            $bankdata = [0 => __('None')];

            foreach ($this->list as $k => &$v) {
                if (!$v['pid']) {
                    continue;
                }
                $bankdata[$v['id']] = $v['name'];
                unset($v['spacer']);
            }
            unset($v);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a",[],'trim');
            $result = $this->model->save($params);

            if ($result === false) {
                $this->error($this->model->getError());
                exit;
            }
            $this->success();

        }
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        
        $row = $this->model->find($ids);

        if(!$row)
        {
            $this->error(__('找不到该课程名称'));
        }

        if ($this->request->isPost()) 
        {
            // 获取表单数据
            $params = $this->request->post("row/a", [], 'trim');

            $params['id'] = $ids;

            if($params)
            {
                $result = $this->model->isUpdate(true)->save($params);

                if($result === false)
                {
                    $this->error($this->model->getError());
                    exit;
                }
                $this->success();
            }
            $this->error(__('参数不为空', ''));
        }

        $this->view->assign("row", $row);

        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $list = $this->model->select($ids);

        if(!$list)
        {
            $this->error('课程名称不存在');
            exit;
        }

        $result = $this->model->destroy($ids);

        if($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }
}
